package com.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.CustomerServerDao;
import com.dao.RoomTypeDao;
import com.dao.ServerLogDao;
import com.model.Customerserver;
import com.model.Roomtype;
import com.model.Serverlog;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 客服登录相关操作类
 *  
 * @author 0912652
 */

public class LoginAction extends ActionSupport implements ModelDriven<Customerserver> {

	private Customerserver custServer = new Customerserver();

	public Customerserver getModel() {
		return custServer;
	}

	private String rand;

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

	
	public String checkCustServer() {
		CustomerServerDao dao = new CustomerServerDao();
		Customerserver newCustServer = dao.checkCustServer(custServer.getSnum(), custServer.getPwd());
		String arandom = (String) (ActionContext.getContext().getSession()
				.get("random"));
		if (newCustServer != null && arandom.equals(this.getRand())) {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("custServerInfo", newCustServer);
			//根据客服权限跳转不同页面
			if(newCustServer.getAuthority()==2){
				List<Customerserver> customerservers = dao.findAll();
				session.setAttribute("allCustServer", customerservers);
				return "Supervisor";
			}else if(newCustServer.getAuthority()==1){
				//客户经理暂时与客服功能一致，如更改时返回manager界面即可
				return "CustServer";
			}else{
				List<Serverlog> serverlogs = new ServerLogDao().findLogBySid(newCustServer.getSid());
				session.setAttribute("serverlog", serverlogs);
				
				List<Roomtype> roomtypes = new RoomTypeDao().findAllTypes();
				session.setAttribute("types", roomtypes);
				return "CustServer";
			}
			
		} else {

			return "Login";
		}
	}

}
